package proj1.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import proj1.dao.Make;
import proj1.dao.Model;
import proj1.dao.Radiator;
import proj1.dao.Radiator.SIZE;
import proj1.dao.Vendor;
import proj1.ui.component.RadiatorTableModel;
import proj1.util.Logger;


public class AddNewRadiator extends JPanel {
	private static Logger log = Logger.getLogger(AddNewRadiator.class);
	private static final long serialVersionUID = 1L;

	public JTable radiatorTable; 

	public AddNewRadiator(final ApplicationFrame mainFrame, final Model selectedModel,final Vendor selectedVendor, final SIZE selectedSize){
		log.info("Selected Model: " + selectedModel + " Selected Vendor: " + selectedVendor + " selected Size: " + selectedSize);
		
		JPanel panel = new JPanel();
		JButton add = new JButton("Add");
		final JComboBox <Vendor> vendor = new JComboBox<>();
		final JComboBox <SIZE> size = new JComboBox<>();
		JPanel comboPanel = new JPanel(new GridLayout(3, 2));
		
		for(SIZE s: SIZE.values()){
			size.addItem(s);
			
		}
		for(Vendor v: Vendor.getVendors()){
			vendor.addItem(v);
		}
		comboPanel.add(new JLabel("Vendor"));
		comboPanel.add(new JLabel("Size"));
		comboPanel.add(vendor);
		comboPanel.add(size);
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		String[][] data = {{"Part Number", ""},
				{"Core", ""},
				{"INHEAD", ""},
				{"OUTHEAD", ""},
				{"INCON", ""},
				{"OUTCON", ""},
				{"TMOUNT", ""},
				{"OILCOOL", ""},
				{"PRICE"," "},
				{"AMOUNT", "" }
			};
		String[] headers = {"Property", "Value"};
		radiatorTable = new JTable(data, headers){
	        private static final long serialVersionUID = 1L;
	        public boolean isCellEditable(int row, int column) {                
	                return column == 1;               
	        };
	    };
		radiatorTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		JScrollPane jsp = new JScrollPane(radiatorTable);
		Dimension d = new Dimension((int) jsp.getPreferredSize().getWidth(), 
									(radiatorTable.getRowHeight()+1)*11);
		jsp.setPreferredSize(d);
		panel.add(comboPanel);
		panel.add(jsp);
		panel.add(add);
		add(panel, BorderLayout.CENTER);
		setVisible(true);
		
		add.addActionListener(new ActionListener() {
			String [] radInfo = new String[10]; 
			Radiator newRad;
			@Override
			//need some way of determining what the rlink is going to be
			public void actionPerformed(ActionEvent e) {
				mainFrame.setMessage("New Radiator Has Been Added");
				//if model is null, then vendor and size are not
				if (selectedModel != null){
					if (radiatorTable.isEditing()) { 
						radiatorTable.getCellEditor().stopCellEditing();  
					}
					for (int i=0; i<radInfo.length; i++){
						radInfo[i] = (String) radiatorTable.getModel().getValueAt(i, 1);
					}
					
					if(radInfo[0].equals("")){
						mainFrame.setErrorMessage("PartNumber cannot be blank, Please enter in number");
					}
					else if(radInfo[0].length() > 3){
						mainFrame.setErrorMessage("Part Number is too long");
					}
					if(radInfo[1].length() > 32){
						mainFrame.setErrorMessage("Core length is too long");
					}
					if(radInfo[2].length() > 17){
						mainFrame.setErrorMessage("InHead length is too long");
									}
					if(radInfo[3].length() > 17){
						mainFrame.setErrorMessage("OutHead length is too long");
					}
					if(radInfo[4].length() > 14){
						mainFrame.setErrorMessage("InCon length is too long");
					}
					if(radInfo[5].length() > 14){
						mainFrame.setErrorMessage("OutCon length is too long");
					}
					if(radInfo[6].length() > 24){
						mainFrame.setErrorMessage("TMount length is too long");
					}
					if(radInfo[7].length() > 10){
						mainFrame.setErrorMessage("OilCool length is too long");
					}
					try{
					newRad = new Radiator(radInfo[0], radInfo[1], radInfo[2], radInfo[3], radInfo[4],
							radInfo[5], radInfo[6], radInfo[7], Double.parseDouble(radInfo[8]), Long.parseLong(radInfo[9]));
					newRad.save(selectedModel,(SIZE) size.getSelectedItem(),(Vendor)vendor.getSelectedItem());
					}
					catch(NumberFormatException ex){
						mainFrame.setErrorMessage("Price and amount must be numbers");
					}
				}
				else{
					
				}
				
			}
		});
	}
	public String toString(){
		return "Fill in fields and click add to add new radiator";
	}
}//Add 





